var doc = document;

function addEvent(target,type,handler){
    if(target.addEventListener){
	target.addEventListener(type,handler,false);
    }
    else{
	target.attachEvent("on"+type,function(event){
	    return handler.call(target,event);
	});
    }
}

function removeEvent(target,type,handler){
    if(target.removeEventListener)
    {
	target.removeEventListener(type,handler,false);
    }
    else{
	target.detachEvent("on"+type,function(event){
	    return handler.call(target,event);
	})
    }
}

window.onload = function(){
    //bind the elements and events
    var download_btn = doc.getElementById("download_btn");
    var close_btn = doc.getElementById("close_btn");
    var close_about_btn = doc.getElementById("close_about_btn");
    var iphone_btn = doc.getElementById("iphone_btn");
    var android_btn = doc.getElementById("android_btn");
    var about_us_btn = doc.getElementById("about_us_btn");
    var toTopBtn = doc.getElementById("toTopBtn");

    addEvent(download_btn,"click",popup_download);
    addEvent(close_btn,"click",popdown);
    //addEvent(close_about_btn,"click",popdown_about)
    addEvent(iphone_btn,"click",popdown);
    addEvent(android_btn,"click",popdown);
    
    addEvent(toTopBtn,"click",scrollToTop);
    //addEvent(about_us_btn,"click",popup_about_us);

    addEvent(contactKeFu,"click",scrollToBottom);
}

function popdown(){
    var popup_holder = doc.getElementById("popup_holder");
    var mask = doc.getElementById("mask");
    popup_holder.style.visibility = "hidden";
    mask.style.visibility = "hidden";
    startScroll();
}

function popdown_about(){
    var about_holder = doc.getElementById("about_holder");
    var mask = doc.getElementById("mask");
    about_holder.style.visibility = "hidden";
    mask.style.visibility = "hidden";
    startScroll();
}

function popup_download(){
    //popup and forbide scroll
    var mask = doc.getElementById("mask");
    var popup_holder = doc.getElementById("popup_holder");
    var win_height = doc.body.clientHeight;
    var win_width = doc.body.clientWidth;
    var scrollTop = doc.body.scrollTop;
    if(scrollTop == 0){
	var scrollTop = doc.documentElement.scrollTop
    }
    popup_holder.style.top = 92+scrollTop+"px";
    mask.style.height = win_height+"px";
    mask.style.width = win_width+"px";
    mask.style.visibility = "visible";
    popup_holder.style.visibility = "visible";
    stopScroll();
    
}

function popup_about_us(){
    var mask = doc.getElementById("mask");
    var about_holder = doc.getElementById("about_holder");
    var win_height = doc.body.clientHeight;
    var win_width = doc.body.clientWidth;
    var scrollTop = doc.body.scrollTop;
    if(scrollTop == 0){
	var scrollTop = doc.documentElement.scrollTop
    }
    mask.style.height = win_height+"px";
    mask.style.width = win_width+"px";
    mask.style.visibility = "visible";
    about_holder.style.top = 92+scrollTop+"px";
    about_holder.style.visibility = "visible";
    stopScroll();
}

/*make the mouse to scroll when close the popup*/
function stopScroll(){
    addEvent(doc,"DOMMouseScroll",forbideScroll);
    document.body.onmousewheel = function(){return false;}
}

/*stop the mouse to scroll when open the popup*/
function startScroll(){
    removeEvent(doc,"DOMMouseScroll",forbideScroll);
    document.body.onmousewheel = function(){}
}

function forbideScroll(e){
    e.preventDefault();
    e.stopPropagation();
}

function scrollToTop(){
    var godBless = doc.body
    if(godBless.scrollTop == 0){
        godBless = doc.documentElement;
    }
    var distanceToTop = godBless.scrollTop;
    var i = 1;
    var scrollTimer = setInterval(function(){
	    godBless.scrollTop = distanceToTop - i;
        i+=30;
        if(godBless.scrollTop <= 0){
            window.clearInterval(scrollTimer);
        }
    },1);
}
